package com.deyuanyun.pic.risk.service.impl;

import com.deyuanyun.pic.common.util.format.NumberFormat;
import com.deyuanyun.pic.risk.controller.dto.RiskAssessmentQuery;
import com.deyuanyun.pic.risk.controller.vo.RiskStatisticsVO;
import com.deyuanyun.pic.risk.domain.RiskAssessment;
import com.deyuanyun.pic.risk.controller.dto.RiskStatisticsQuery;
import com.deyuanyun.pic.risk.mapper.RiskAssessmentDao;
import com.deyuanyun.pic.risk.service.RiskAssessmentService;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 风险评价 服务接口实现类
 *
 * @author huanglw
 * @date 2016-7-19
 *
 * History: [修改人][时间][修改描述]
 */
@Service
public class RiskAssessmentServiceImpl implements RiskAssessmentService {

	@Resource
	RiskAssessmentDao riskAssessmentDao;

	@Override
	public RiskStatisticsVO getRiskStatisticalResults(RiskStatisticsQuery riskStatisticsQuery) {

		BigDecimal countHighRisk = riskAssessmentDao.countHighRisk(riskStatisticsQuery);//高风险 风险评价管段 条数
		BigDecimal countControlMeasures = riskAssessmentDao.countControlMeasures(riskStatisticsQuery);//已经采取措施的 风险评价管段 条数
		BigDecimal countAllRisk = riskAssessmentDao.countAllRisk(riskStatisticsQuery);
		// “高风险管段”占比
		String highRiskProportion = calculationProportion(countHighRisk, countAllRisk);
		// 最大风险信息
		List<Map> maxRiskList = riskAssessmentDao.findMaxRisk(riskStatisticsQuery);
		// “已采取措施管段”占比
		String controlMeasuresProportion = calculationProportion(countControlMeasures, countAllRisk);
		// 查询风险管段信息
		List<RiskAssessment> riskAssessmentList = riskAssessmentDao.queryRiskPipeSegment(riskStatisticsQuery);
		// 统计风险等级
		List<Map> riskGradeList = riskAssessmentDao.queryRiskGrade(riskStatisticsQuery);

//		List pipeSegmentList = new ArrayList();
//		String relativeRiskScore = "0";
//		for (Map map : maxRiskList) {
//			pipeSegmentList.add(map.get("riskName".toUpperCase()));
//			relativeRiskScore = NumberFormat.format((BigDecimal)map.get("relativeRiskScore".toUpperCase()));
//		}
		String riskName = "";
		String relativeRiskScore = "0";
		if (maxRiskList != null && !maxRiskList.isEmpty()) {
			Map map = maxRiskList.get(0);
			riskName = (String) map.get("riskName".toUpperCase());
			relativeRiskScore = NumberFormat.format((BigDecimal)map.get("relativeRiskScore".toUpperCase()));
		}

		RiskStatisticsVO riskStatisticsVO = new RiskStatisticsVO(highRiskProportion, riskName, relativeRiskScore, controlMeasuresProportion);
		riskStatisticsVO.fromRiskAssessment(riskAssessmentList);
		riskStatisticsVO.fromRiskGrade(riskGradeList);

		return riskStatisticsVO;
	}

	@Override
	public Page<RiskAssessment> queryRiskAssessment(RiskAssessmentQuery riskAssessmentQuery) {
		return riskAssessmentDao.queryRiskAssessment(riskAssessmentQuery);
	}


	/**
	 * 计算百分比
	 *
	 * @param divisor 除数
	 * @param dividend 被除数
     * @return
     */
	private String calculationProportion(BigDecimal divisor, BigDecimal dividend){
		return NumberFormat.format(dividend.compareTo(new BigDecimal(0))==0 ?
				new BigDecimal(0) : divisor.multiply(new BigDecimal(100)).divide(dividend, 4, BigDecimal.ROUND_HALF_EVEN))+" %";
	}
}
